//
//  NewModifyPwdView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/26.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#define kDefalultTFViewFrame CGRectMake(0, 0, SCREEN_WIDTH, 50)


#import "NewModifyPwdView.h"

//左右距离controller的间距为 15
//h: 40*4 + 5*3 + 20 =  195
@interface NewModifyPwdView()
/**登录 跳转到登录注册页面*/
@property (nonatomic, strong) UIButton *loginBtn;

@end

@implementation NewModifyPwdView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
 
    [self addSubview:self.oldPwdTF];
    [self addSubview:self.xinPwdTF];
    [self addSubview:self.surePwdTF];
    [self addSubview:self.loginBtn];
}

#pragma mark - 二 getter
- (FDSameToAndroidTextFieldCustomView *)oldPwdTF {
    if (!_oldPwdTF) {
        _oldPwdTF = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:kDefalultTFViewFrame title:NSLocalizedString(PleaseInPutYouOldPwd, @"请输入旧密码")];
//        _oldPwdTF.tf.secureTextEntry = YES;//默认密文
    }
    return _oldPwdTF;
}

- (FDSameToAndroidTextFieldCustomView *)xinPwdTF {
    if (!_xinPwdTF) {
        _xinPwdTF = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:kDefalultTFViewFrame title:NSLocalizedString(PleaseSetNewPwd, @"请设置新的登录密码")];
        _xinPwdTF.tf.secureTextEntry = YES; //默认密文
//        _xinPwdTF.rightViewMode = UITextFieldViewModeAlways; //不写不显示
        
        
        //右边的眼睛
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [btn setImage:[UIImage imageNamed:@"by"] forState:(UIControlStateNormal)];
        [btn setImage:[UIImage imageNamed:@"zy"] forState:(UIControlStateSelected)];
        [btn addTarget:self action:@selector(clickZhenYanBiYanAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_xinPwdTF.topBgView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self->_xinPwdTF.topBgView);
            make.width.mas_equalTo(40);
        }];
    }
    return _xinPwdTF;
}

- (FDSameToAndroidTextFieldCustomView *)surePwdTF {
    if (!_surePwdTF) {
        _surePwdTF = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:kDefalultTFViewFrame title:NSLocalizedString(PleaseConfirmNewPwd, @"请确认新的登录密码")];
        _surePwdTF.tf.secureTextEntry = YES;
    }
    return _surePwdTF;
}


- (UIButton *)loginBtn {
    if (!_loginBtn) {
        //2019-2-25 修改为 Confirm
        NSString *str = @"登录";
        if ([NSString rb_ChineseLanguage]) {
            str = @"Confirm";
        }

        _loginBtn = [UIButton createFastRedBtnRadius:4.0 target:self action:@selector(clickLoginBtnAction:) title:str];
        
        
        [_loginBtn setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];
    }
    return _loginBtn;
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat h = 50;
    [self.oldPwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.mas_top);
        make.height.mas_equalTo(h);
    }];
    
    [self.xinPwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.oldPwdTF.mas_bottom).offset(5);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(h);
    }];
    
    [self.surePwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.xinPwdTF.mas_bottom).offset(5);
        make.height.mas_equalTo(h);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(44);
        make.top.equalTo(self.surePwdTF.mas_bottom).offset(30);
    }];
}

#pragma mark - 四 点击事件

- (void)clickZhenYanBiYanAction:(UIButton *)btn {
    FDLog(@"睁眼闭眼");
    

    if (btn.selected) {
        self.xinPwdTF.tf.secureTextEntry = YES;
        self.surePwdTF.tf.secureTextEntry = YES;
    }else {
        self.xinPwdTF.tf.secureTextEntry  = NO;
        self.surePwdTF.tf.secureTextEntry = NO;
    }
    
    btn.selected = !btn.selected;
}

- (void)clickLoginBtnAction:(UIButton *)btn {
    FDLog(@"点击登录");
    
    if (self.clickLoginBlock) {
        self.clickLoginBlock();
    }
}

@end
